package algorithmTopics.array;

import java.io.*;
import java.util.*;

public class Main {

    public static void main(String[] args) throws IOException {
        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
        // 数组个数M
        int m = Integer.parseInt(reader.readLine().trim());
        ArrayList<int[]> reault = new ArrayList<>();
        if (m > 10 || m < 1) {
            System.out.println("[[]]");
            System.exit(0);
        }
        for (int i = 0; i < m; i++) {
            String s = reader.readLine();
            if (s == null || s.equals("")) {
                break;
            }
            int[] array = parseIntArray(s);
            int[] arrayed = arraySortAndSet(array);
            reault.add(arrayed);
        }
        ArrayList<int[]> ints = mergeGroupSame(reault);
        System.out.print(toString(ints));


    }
    //判断组间是否有相同的,进行合并
    private static ArrayList<int[]> mergeGroupSame(ArrayList<int[]> reault){
        ArrayList<int[]> list = new ArrayList<>();
        HashSet<Integer> helper = new HashSet<>();
        for (int i = 0; i < reault.size() - 1; i++) {
            int[] now = reault.get(i);
            int[] next = reault.get(i + 1);
            if (now.length < 2 || next.length < 2) {
                continue;
            }
            for (int j = 0; j < now.length; j++) {
                helper.add(now[j]);
            }
            for (int j = 0; j < next.length; j++) {
                helper.add(now[j]);
            }
            if (helper.size() - (now.length + next.length) >= 2) {
                //便是 可以合并这俩 ,合并的结果就是helper
                Object[] objects = helper.toArray();
                int[] ints = new int[objects.length];
                for (int j = 0; j < objects.length; j++) {
                    ints[i] = (int) objects[i];
                }
                Arrays.sort(ints);
                list.add(ints);
                i++;
                continue;
            } else {
                list.add(now);
                continue;
            }

        }
        if (list.size() != reault.size()) {
            return mergeGroupSame(list);
        }

        return list;
    }

    private static String toString(ArrayList<int[]> reault) {
        String res = "[";
        if (reault.size() == 0) {
            return "[[]]";
        }
        for (int i = 0; i < reault.size(); i++) {
            int[] ints = reault.get(i);
            String l = "[";
            for (int j = 0; j < ints.length; j++) {
                if (j == 0) {
                    l += ints[j];
                } else {
                    l = l + "," + ints[j];
                }
                if (j == ints.length - 1) {
                    l += "]";
                }
            }
            if (i == 0) {
                res += l;
            } else {
                res = res + "," + l;
            }
            if (i == reault.size() - 1) {
                res += "]";
            }
        }
        return res;


    }


    // 数组去重并排序
    private static int[] arraySortAndSet(int[] array) {
        Arrays.sort(array);
        Set<Integer> resTemp = new HashSet<>();
        for (int i = 0; i < array.length; i++) {
            resTemp.add(array[i]);
        }
        int[] res = new int[resTemp.size()];
        Object[] objects = resTemp.toArray();
        for (int i = 0; i < resTemp.size(); i++) {
            res[i] = (int) objects[i];
        }
        return res;
    }

    // 字符串转化为Int 数组
    private static int[] parseIntArray(String line) {
        String[] split = line.split(",");
        if (split.length > 100 || split.length < 1) {
            System.out.println("[[]]");
            System.exit(0);
        }
        int[] result = new int[split.length];
        for (int i = 0; i < split.length; i++) {
            int num = Integer.parseInt(split[i]);
            result[i] = num;
        }
        return result;
    }
}
